package cn.com.jslh.boarpro.main.model;

import android.content.Context;

import com.blankj.utilcode.util.SPUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.com.jslh.boarpro.base.BaseApplication;
import cn.com.jslh.boarpro.base.mvp.BaseListModel;
import cn.com.jslh.boarpro.main.model.breeditems.BreedItem;
import cn.com.jslh.boarpro.util.JSONUtil;
import cn.com.jslh.boarpro.util.WebService;
import cn.com.jslh.boarpro.util.database.DataBaseUtil;

/**
 * 作者：周洲 on 2017/7/26 11:02
 * <p>
 * 邮箱：double0zhou@126.com
 */

public class BreedingRecordModelImpl extends BaseListModel {
    //    public static String week ="#";
//    public static String pzdate = "#";
//    public static String ycdate = "#";

    public BreedingRecordModelImpl(final Context context) {
        super(context);
//        if(week.equals("#")) {
//            week = ZhouHaoUtil.getZhouHao(context, TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")));
//        }
//        if (ycdate.equals("#")) {
//            ycdate = DateUtil.getOldDate(114);
//        }
    }

    public void getBaseAsyncDatas(final WebService.NetSuccessListener netSuccessListener){
//        AppUtil.judgeDate(context, new WebService.NetSuccessListener() {
//            @Override
//            public void Success(String s) throws JSONException {
//                JSONObject jsonObject = new JSONObject(s);
//                String flag = jsonObject.getString("flag");
//                if (!flag.equals("1")) {
//                    Toast.makeText(context, "当前所选日期不符合规则,请联系管理员", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//                pzdate = TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd"));
//                netSuccessListener.Success(pzdate);
//            }
//        }, TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")), SPUtils.getInstance().getString(BaseApplication.FORGID), "FenMian");
    }

    @Override
    public List<?> getDatas(String... params) {
        List<BreedItem> datas = new ArrayList<>();
        //周号
//        datas.add(BreedItemFactory.createBreedItem(new BreedItem.DataTemplates("周号", week, "zhouhao", null, BreedItemFactory.TextBreedItem)));
        //配种日期
//        datas.add(BreedItemFactory.createBreedItem(new BreedItem.DataTemplates("配种日期", pzdate, "peizhongriqi", null, BreedItemFactory.DateBreedItem)));
//        //预产期
//        datas.add(BreedItemFactory.createBreedItem(new BreedItem.DataTemplates("预产期", ycdate, "yuchanriqi", null, BreedItemFactory.TextBreedItem)));
        return datas;
    }

    /**
     * 获取数据库中存入的配种记录
     *
     * @return
     */
    public List<JSONObject> getUIDatas() {
        JSONArray jsonArray = DataBaseUtil.GetData(context, "select * from BreedRecordTable  where lineID = '"+ SPUtils.getInstance().getString(BaseApplication.FLINEID)+"' order by _id desc");
        return JSONUtil.JsonArray2List(jsonArray);
    }

    /**
     * 提交记录数据
     *
     * @param jsonStr
     * @param nListener
     */
    public void syncUpData(String jsonStr, WebService.NetSuccessListener nListener, WebService.NetFaildListener netFaildListener) {
        new WebService.Builder(context, "importPeiZhongData", "WSSowFacade")
                .showDialog(true)
                .addProperty("jsonString", jsonStr)
                .success(nListener)
                .faild(netFaildListener)
                .post();
    }

}
